package com.ruoyi.mongo.util;

import com.mongodb.client.AggregateIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.ruoyi.mongo.souce.MongoDB04AuthPoolUtil;

import org.bson.Document;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * $project 投影操作
 */
public class ProjectOper {
    /**
     * 查询 dev 集合,将数组中的内容拆分显示,并只显示title 键与 tags 键中的
     *  db.dev.aggregate([{$unwind:"$tags"},{$project:{_id:0,tags:"$tags",title:"$title"}}])
     */
    public void  selectDocumentProject(){
        MongoCollection collection = MongoDB04AuthPoolUtil.getCollection("develop","dev");
        Document unwind = new Document();
        unwind.put("$unwind","$tags");
        Document pro = new Document();
        pro.put("_id",0);
        pro.put("tags","$tags");
        pro.put("title","$title");
        Document project = new Document();
        project.put("$project",pro);
        List<Document> list = new ArrayList<>();
        list.add(unwind);
        list.add(project);
        AggregateIterable iterable = collection.aggregate(list);
        MongoCursor<Document> cursor = iterable.iterator();
        while(cursor.hasNext()){
            // 每掉一次next移动以下指针
           // Document docu = cursor.next();
            System.out.println(cursor.next());
        }
    }

    /**
     * 查询 dev集合,将数组中的内容拆分显示.将 title字段和 tags字段的值拼接为一个完整字符串并在Title_Tags 字段中显示
     *  db.dev.aggregate([{$unwind,"$tags"},{$project:{_id:0,Title_Tags:{$concat:["$title","_","$tags"]}}}])
     */
    public void  selectDocumentProjectConcat(){
        MongoCollection collection = MongoDB04AuthPoolUtil.getCollection("develop","dev");
        Document unwind = new Document();
        unwind.put("$unwind","$tags");

        Document concat = new Document();
        concat.put("$concat", Arrays.asList(new String[]{"$title","_","$tags"}));
        Document pro = new Document();
        pro.put("_id",0);
        pro.put("Title_Tags",concat);
        Document project = new Document();
        project.put("$project",pro);
        List<Document> list = new ArrayList<>();
        list.add(unwind);
        list.add(project);
        AggregateIterable iterable = collection.aggregate(list);
        MongoCursor<Document> cursor = iterable.iterator();
        while(cursor.hasNext()){
            // 每掉一次next移动以下指针
            // Document docu = cursor.next();
            System.out.println(cursor.next());
        }
    }

    /**
     *  查询dev集合中数据,显示title和size字段,为 size 字段数据做加1操作,显示字段命名为 New_Size,排除那些内有size键的文档
     *   db.dev.aggregate([{$match:{size:{$ne:null}}},{$project:{_id:0,title:1,New_Size:{$add:["$size",1]}}}])
     */
    public void  selectDocumentProjectAdd(){
        MongoCollection collection = MongoDB04AuthPoolUtil.getCollection("develop","dev");
        Document ne = new Document();
        ne.put("$ne",null);
        Document size = new Document();
        size.put("size",ne);
        Document match = new Document();
        match.put("$match",size);

        Document add = new Document();
        add.put("$add",Arrays.asList(new Object[]{"$size",1}));
        Document pro = new Document();
        pro.put("_id",0);
        pro.put("title",1);
        pro.put("New_Size",add);
        Document project = new Document();
        project.put("$project",pro);
        List<Document> list = new ArrayList<>();
        list.add(match);
        list.add(project);
        AggregateIterable iterable = collection.aggregate(list);
        MongoCursor<Document> cursor = iterable.iterator();
        while(cursor.hasNext()){
            // 每掉一次next移动以下指针
            // Document docu = cursor.next();
            System.out.println(cursor.next());
        }
    }

    /**
     * 查询devtest 集合查询那些有生日的用户,并按照YYYY年mm月dd日 HH:MM:SS 格式显示日期
     * db.devtest.aggregate([{$match:{userbirth:{$ne:null}}},{$project:{自定义日期格式:{$dateToString:{format:"%Y年%m月%d日 %H:%M:%S",date:"userbirth"}}}}])
     * 注意: 如果直接在MongoDB中做日期的格式化处理,那么是按照标准UTC时间来处理的,会少8个小时.建议在程序中
     * 通过java.util.Date来做日期的转换.
     */
    public void  selectDocumentProjectDate(){
        MongoCollection collection = MongoDB04AuthPoolUtil.getCollection("develop","devtest");
        Document ne = new Document();
        ne.put("$ne",null);
        Document userbirth = new Document();
        userbirth.put("userbirth",ne);
        Document match = new Document();
        match.put("$match",userbirth);

        Document format = new Document();
        format.put("format","%Y年%m月%d日 %H:%M:%S");
        format.put("date","$userbirth");
        Document dateToString = new Document();
        dateToString.put("$dateToString",format);
        Document pro = new Document();
        pro.put("自定义日期格式",dateToString);
        Document project = new Document();
        project.put("$project",pro);
        List<Document> list = new ArrayList<>();
        list.add(match);
        list.add(project);
        AggregateIterable iterable = collection.aggregate(list);
        MongoCursor<Document> cursor = iterable.iterator();
        while(cursor.hasNext()){
            // 每掉一次next移动以下指针
            // Document docu = cursor.next();
            System.out.println(cursor.next());
        }
    }


    public static void main(String[] args) {
        ProjectOper projectOper = new ProjectOper();
        projectOper.selectDocumentProjectDate();
    }


}
